<template>

    <div>
        <div class="headerL" style="display:flex;height:40px;background:#000"><span style="color:#FFF" @click="back">＜返回</span><b style="color:#fff">地址管理</b></div>
        <van-address-edit
  :area-list="areaList"
  :address-info='AddressInfo'
  show-postal
  show-delete
  show-set-default
  show-search-result
  :search-result="searchResult"
  :area-columns-placeholder="['请选择', '请选择', '请选择']"
  @save="onSave"
  @delete="onDelete"
  @change-detail="onChangeDetail"
/>
    </div>
</template>

<script>
import Vue from 'vue'
import axios from 'axios'
import city from '@/utils/city.js'
import { AddressEdit, Toast } from 'vant'
Vue.use(AddressEdit)
Vue.use(Toast)
export default {
  data () {
    return {
      areaList: city,
      searchResult: [],
      AddressInfo: {}
    }
  },

  methods: {
    onSave (content) {
      console.log(content)
      let addressid = this.$route.query.addressid
      if (addressid) {
        axios.post('/address/update', {
          addressid: addressid,
          userid: localStorage.getItem('userid'),
          name: content.name,
          tel: content.tel,
          address: content.province + content.city + content.county + content.addressDetail,
          flag: content.isDefault
        }).then(() => {
          Toast('修改地址成功')
          this.$router.push('/address')
        })
      } else {
        axios.post('/address/add', {
          userid: localStorage.getItem('userid'),
          name: content.name,
          tel: content.tel,
          address: content.province + content.city + content.county + content.addressDetail,
          flag: content.isDefault
        }).then(() => {
          Toast('添加新地址成功')
          this.$router.push('/address')
        })
      }
    },
    onDelete () {
      Toast('删除地址成功')
      let addressid = this.$route.query.addressid
      console.log(addressid)
      axios.get('/address/delete?addressid=' + addressid)
      this.$router.push('/address')
    },
    onChangeDetail (val) {
    },
    onChangeDefault (val) {
    },
    back () {
      this.$router.go(-2)
    }
  },
  created () {
    let addressid = this.$route.query.addressid
    if (addressid) {
      axios.get('/address?addressid=' + addressid + '&userid=' + localStorage.getItem('userid')).then(res => {
        let Dz = false
        if (res.data.data[0].flag === 1) {
          Dz = true
        }
        this.AddressInfo = {
          name: res.data.data[0].name,
          tel: res.data.data[0].tel,
          province: res.data.data[0].address,
          addressDetail: res.data.data[0].address,
          postalCode: 476800,
          isDefault: Dz
        }
      })
    }
  }
}
</script>

<style lang="scss">
@import '@/lib/reset.scss';
.headerL{
    line-height: 40px;
      span{
      display: inline-block;
      text-indent: 10px;
    }
    b{
      display: inline-block;
      @include flex(2);
      text-align: left;
      text-indent: 1.2rem;
    }
}
</style>
